<?php

class Contact extends CI_Controller {

    private static $layout_data = array();
    private static $home_data = array();
    
    public function __construct()
    {
        parent::__construct();
        
        self::$home_data = array();
        self::$layout_data['page_title'] = 'Oneway.vn - Liên Hệ';
    }
	
    public function index()
    {
        self::$layout_data['content'] = $this->load->view('contact/index', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }

    public function tiepnhan()
    {
        self::$layout_data['content'] = $this->load->view('contact/tiepnhan', self::$home_data, TRUE);
        $this->load->view('layouts/main', self::$layout_data);
    }
    
    /**
     * Send contact Action
     */
    public function sendContact()
    {
        //Get params
        $fullname = $this->input->post('fullname');
        $sender = $this->input->post('sender');
        $address = $this->input->post('address');
        $mobile = $this->input->post('mobile');
        $age = $this->input->post('age');
        $job = $this->input->post('job');
        $title = $this->input->post('title');
        $content = $this->input->post('content');
        $reciver = 'lienhe@oneway.vn';

        //Set response default
        $response = array(
            'error'     => 1,
            'message'   => 'Vui lòng điền đầy đủ thông tin.'
        );

        if(empty($fullname) || empty($sender) || empty($content) || empty($reciver) || empty($age))
        {
            echo json_encode($response); exit();
        }

        $this->load->helper('email');

        if (!valid_email($sender) || !valid_email($reciver))
        {
            $response['message'] = 'Email không hợp lệ';
            echo json_encode($response); exit();
        }

        //Ser content
        $mailContent = "Họ tên: ".$fullname."<br/>";
        $mailContent.= "Địa chỉ: ".$address."<br/>";
        $mailContent.= "Điện thoại: ".$mobile."<br/>";
        $mailContent.= "Tuổi: ".$age."<br/>";
        $mailContent.= "Nghề nghiệp: ".$job."<br/>";
        $mailContent.= "Nội dung: ".$content;
        
        //Set email config
        $config['useragent']	= "Oneway.vn";
        $config['protocol']		= "smtp";	
        $config['smtp_host']    = "mail.dibui.vn";
        $config['smtp_user']	= "admin@dibui.vn";
        $config['smtp_pass']	= "s@young~";
        $config['smtp_port']    = 25;

        $this->load->library('email', $config);
        
        //var_dump($this->email);exit;

        $this->email->from($sender, $fullname);
        $this->email->to($reciver);
        $this->email->subject('Liên hệ - '.$title);
        $this->email->message($mailContent);

        $result = $this->email->send();

        //Set response
        $response = array(
            'error'     => $result?0:1,
            'message'   => $result?'Success':'Error'
        );

        //Return data
        echo json_encode($response); exit();
    }

}
